package com.glory.brepda.adapter;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.TempRecipeEntity;

import java.util.List;

/**
 * FileName: TempRecipeListAdapter
 * Author: XJH
 * Date: 2021/4/9 11:48
 * Description:
 * History:
 */
public class TempRecipeListAdapter extends BaseQuickAdapter<TempRecipeEntity.TempRecipeInfoEntity, BaseViewHolder> {
    private onRecipeSelectorListener mListener;
    public TempRecipeListAdapter(@Nullable List<TempRecipeEntity.TempRecipeInfoEntity> data,onRecipeSelectorListener listener) {
        super(R.layout.adapter_temp_recipe,data);
        this.mListener = listener;
    }
    private int mSelectorPos = -1;
    public void setmSelectorPos(int mSelectorPos){
        this.mSelectorPos = mSelectorPos;
    }
    @Override
    protected void convert(BaseViewHolder helper, TempRecipeEntity.TempRecipeInfoEntity item) {
        helper.setText(R.id.tvEqpID,item.getEquipmentId())
                .setText(R.id.tvRecipeName,item.getRecipeName())
                .setText(R.id.tvRecipeVersion,item.getVersion())
                .setText(R.id.tvRecipeType,item.getEquipmentType());

        helper.setBackgroundColor(R.id.llMain,item.isChecked() ?
                MyApp.getInstance().getResources().getColor(R.color.free_ui_glory_theme_color) :
                MyApp.getInstance().getResources().getColor(R.color.white));

        helper.getView(R.id.llMain).setOnClickListener(view -> {
            if (mSelectorPos == -1){
                mSelectorPos = helper.getAdapterPosition();
                item.setChecked(true);
            }else if (mSelectorPos == helper.getAdapterPosition()){
                return;
            }else if (mSelectorPos != helper.getAdapterPosition()){
                getData().get(mSelectorPos).setChecked(false);
                item.setChecked(true);
                mSelectorPos = helper.getAdapterPosition();
            }
            mListener.onSelectorListener(item.getRecipeName(),item.getId());
            notifyDataSetChanged();
        });
    }

    public interface onRecipeSelectorListener{
        void onSelectorListener(String recipeName,int id);
    }
}
